package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.entity.views.ReNewDetailForTeacher;
import com.xmy.cultivate.entity.views.ResidueStatics;
import com.xmy.cultivate.mapper.*;
import com.xmy.cultivate.service.IOrderDetailService;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.IRenewDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.IStudentTypeService;
import com.xmy.cultivate.util.ConfigData;
import com.xmy.cultivate.util.DateUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author min
 * @since 2022-11-29
 */
@Service
public class RenewDataServiceImpl extends ServiceImpl<RenewDataMapper, RenewData> implements IRenewDataService {

    @Autowired
    StaffMapper staffMapper;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    RenewDataMapper renewDataMapper;

    @Autowired
    IStudentTypeService iStudentTypeService;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Autowired
    QuarterMapper quarterMapper;

    @Autowired
    @Lazy
    CourseMapper courseMapper;

    @Autowired
    @Lazy
    GradeMapper gradeMapper;

    @Autowired
    StudentGradeMapper studentGradeMapper;

    @Autowired
    StudentMapper studentMapper;


    public boolean addData(Long school_id, Long student_id,Integer yearPart,Course course, Grade grade){
        return true;
    }


    /**
     * 续班类型：1报名，续费，2转课，3转班，4转校
     * @param school_id
     * @param student_id
     * @param course
     * @param grade
     * @param courseHouse
     * @param reNewType
     * @return
     */
    public boolean updateData(Long school_id, Long student_id,Course course, Grade grade,Integer courseHouse,Integer reNewType){

        return true;
    }


    /**
     * 分班更新老师
     * @param schoolId
     * @param studentId
     * @param course
     * @param grade
     * @return
     */
    public boolean updateTeacher(Long schoolId, Long studentId,Course course, Grade grade){
        RenewData renewData = this.getRenewDataForSubjects(schoolId,course.getSubjectsId(),studentId,grade.getYearPart(),course.getQuarterNum(),1);

        return true;
    }





    /**
     * 转校更新续班信息
     * @param studentId
     * @param outSchoolId
     * @param inSchoolId
     * @param outCourseId
     * @param inCourseId
     * @param inGradeId
     * @param inCourseHosue
     * @return
     */
    public boolean transferSchoolUpdate(Long studentId,Long outSchoolId,Integer outYearPart,Long inSchoolId,Long outCourseId,Long inCourseId, Long inGradeId,Integer inCourseHosue){

        return true;
    }


    public boolean deleteData(Long schoolId, Long studentId,Long subjectsId,Integer yearPart,Integer quarterNum){
        QueryWrapper<OrderDetail> orderDetailQueryWrapper = new QueryWrapper<>();
        orderDetailQueryWrapper.eq("school_id",schoolId);
        orderDetailQueryWrapper.eq("student_id",studentId);
        orderDetailQueryWrapper.eq("subjects_id",subjectsId);
        orderDetailQueryWrapper.eq("year_part",yearPart);
        orderDetailQueryWrapper.eq("quarter_num",quarterNum);
        orderDetailQueryWrapper.ne("status",2);
        orderDetailQueryWrapper.eq("deleted",0);

        Long count = orderDetailMapper.selectCount(orderDetailQueryWrapper);
        if(count==0){
            renewDataMapper.delete(new QueryWrapper<RenewData>().eq("school_id",schoolId).eq("subjects_id",subjectsId).eq("student_id",studentId).eq("year_part",yearPart).eq("quarter_num",quarterNum));
        }
        return true;
    }

    /**
     * 把学生从班级里删除，更新老师数据
     * @param
     * @param teacherId
     * @param studentId
     * @param subjectsId
     * @param yearPart
     * @param quarterNum
     * @return
     */
    public boolean deleteTeacherForGradeDelStudent(Grade grade, Long teacherId,Long studentId,Long subjectsId,Integer yearPart,Integer quarterNum){

        QueryWrapper<RenewData> renewDataQueryWrapper = new QueryWrapper<>();
        renewDataQueryWrapper.eq("school_id",grade.getSchoolId());
        renewDataQueryWrapper.eq("student_id",studentId);
        renewDataQueryWrapper.eq("subjects_id",subjectsId);
        renewDataQueryWrapper.eq("year_part",yearPart);
        renewDataQueryWrapper.eq("quarter_num",quarterNum);
        renewDataQueryWrapper.eq("teacher_id",teacherId);

         List<RenewData> renewDataList = renewDataMapper.selectList(renewDataQueryWrapper);

        if(renewDataList.size()>0){
            QueryWrapper<StudentGrade> studentGradeQueryWrapper = new QueryWrapper<>();
            studentGradeQueryWrapper.eq("school_id",grade.getSchoolId());
            studentGradeQueryWrapper.eq("subjects_id",subjectsId);
            studentGradeQueryWrapper.eq("year_part",yearPart);
            studentGradeQueryWrapper.eq("quarter_num",quarterNum);
            studentGradeQueryWrapper.eq("deleted",0);
            studentGradeQueryWrapper.ne("status",2);

            Long sgCount = studentGradeMapper.selectCount(studentGradeQueryWrapper);

            if(sgCount == 0){
                for (RenewData renewData:renewDataList)
                {
                    if(renewData.getTeacherId().equals(grade.getTeacherId())){
                        renewData.setTeacherId(0L);
                        renewData.setTeacherName("");
                        renewData.setIsRestor(0);
                        int rs = renewDataMapper.updateById(renewData);
                    }
                }
            }
        }
        return true;
    }



    /**
     * 获取有没有添加过报读的续班信息
     * @param schoolId
     * @param subjectsId
     * @param studentId
     * @param yearPart
     * @param quarterNum
     * @param type
     * @return
     */
    public RenewData getRenewDataForSubjects(Long schoolId,Long subjectsId,Long studentId,Integer yearPart,Integer quarterNum,Integer type){
        QueryWrapper<RenewData> renewDataQueryWrapper = new QueryWrapper<>();
        renewDataQueryWrapper.eq("school_id",schoolId);
        renewDataQueryWrapper.eq("subjects_id",subjectsId);
        renewDataQueryWrapper.eq("student_id",studentId);
        renewDataQueryWrapper.eq("year_part",yearPart);////////////////////
        renewDataQueryWrapper.eq("quarter_num",quarterNum);
        renewDataQueryWrapper.eq("type",type);
        RenewData renewData = renewDataMapper.selectOne(renewDataQueryWrapper);
        return  renewData;
    }

    /**
     * 获取有没有续班信息
     * @param schoolId
     * @param subjectsId
     * @param studentId
     * @param yearPart
     * @param quarterNum
     * @param type
     * @return
     */
    public RenewData getRenewDataForRenew(Long schoolId,Long subjectsId,Long studentId,Integer yearPart,Integer quarterNum,Integer type){
        QueryWrapper<RenewData> renewDataQueryWrapper = new QueryWrapper<>();
        renewDataQueryWrapper.eq("school_id",schoolId);
        renewDataQueryWrapper.eq("subjects_id",subjectsId);
        renewDataQueryWrapper.eq("student_id",studentId);
        renewDataQueryWrapper.eq("year_part_renew",yearPart);//////
        renewDataQueryWrapper.eq("quarter_num_renew",quarterNum);
        renewDataQueryWrapper.eq("type",type);
        RenewData renewData = renewDataMapper.selectOne(renewDataQueryWrapper);
        return  renewData;
    }


    public IPage<ReNewDetailForTeacher> getRnewDetailForTeahcer(IPage<?> page, Wrapper wrapper){
        IPage<ReNewDetailForTeacher> reNewDetailForTeacherList = renewDataMapper.getStudentDetailList(page,wrapper);
        for (ReNewDetailForTeacher reNewDetailForTeacher:reNewDetailForTeacherList.getRecords()){
            Student student = studentMapper.selectById(reNewDetailForTeacher.getStudentId());
            if(student != null){
                reNewDetailForTeacher.setStudentName(student.getName());
            }else {
                //System.out.println("teacher_id:"+reNewDetailForTeacher.getStudentId());
            }

            reNewDetailForTeacher.setTeachTeacherNameList(studentGradeMapper.getGradeTeacherNameList(reNewDetailForTeacher.getSchoolId(), reNewDetailForTeacher.getStudentId(),reNewDetailForTeacher.getSubjectsId(),reNewDetailForTeacher.getYearPart(),reNewDetailForTeacher.getQuarterNum()));

        }
        return reNewDetailForTeacherList;
    }

    public List<ResidueStatics> residueStudentCount(QueryWrapper queryWrapper){
        return renewDataMapper.residueStudentCount(queryWrapper);
    }

}
